package com.jinsi.controller.ylt;

import com.google.common.collect.Lists;
import com.jinsi.constant.YltConstant;
import com.jinsi.pojo.*;
import com.jinsi.service.ylt.IndexService;
import com.jinsi.service.yrf.AdministratorLoginLogService;
import com.jinsi.util.R;
import com.jinsi.util.RedisUtils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @ClassName IndexController
 * @Content 首页左侧导航栏Controller
 * @Author 姚龙腾
 * @Date 2022/10/12 11:53
 */
@RestController
@Slf4j
public class IndexController {
    @Autowired
    private IndexService indexService;
    @Autowired
    private AdministratorLoginLogService administratorLoginLogService;
    @Autowired
    private RedisUtils redisUtils;
    @Value("${HttpServer}")
    private String httpUrl;


    /**
     * 获取左侧导航栏
     * @return Init
     */
    @RequestMapping("/getMenu")
    @ResponseBody
    public Init getMenu(HttpServletRequest servletRequest){
        //从session中拿出当前用户信息
        SyUser user = (SyUser) servletRequest.getSession().getAttribute("user");

        //添加登录日志
        try {
            administratorLoginLogService.insertadministratorLoginLog(user);
            log.info("添加登录日志成功,用户账号为:"+user.getUserAccount());
        } catch (Exception e) {
            log.error("添加登录日志失败",e);
        }
        //从security上下文中拿出当前用户权限
        Collection<? extends GrantedAuthority> authorities = SecurityContextHolder.getContext().getAuthentication().getAuthorities();
        //转成ArrayList 方便进行操作
        ArrayList<Object> classPoList = Lists.newArrayList(authorities);

        //对layuimini 框架的首页数据进行对象封装
        InfoData homeInfo = new InfoData();
        homeInfo.setTitle(YltConstant.INDEX_TITLE);
        homeInfo.setHref(YltConstant.INDEX_HREF);
        InfoData logoInfo = new InfoData();
        logoInfo.setTitle("");
        //判断当前用户是否拥有头像 如果没有
        if (user.getUserUrl() == null){
            //根据用户性别进行展示设置好的男女头像
            //如果用户性别也为空 展示默认头像
            switch (user.getUserSex()){
                case YltConstant.GIRL_SEX:
                    logoInfo.setImage(YltConstant.GIRL_IMG);
                    break;
                case YltConstant.MAN_SEX:
                    logoInfo.setImage(YltConstant.MAN_IMG);
                    break;
                default:
                    logoInfo.setImage(YltConstant.DEFAULT_IMG);
            }
        }else {
            //如果用户拥有头像 就填充进入展示左侧上方
            String url = httpUrl + user.getUserUrl();
            logoInfo.setImage(url);
        }
        logoInfo.setHref("");

        //对layuimini 框架的首页数据进行对象封装
        Init init = new Init();
        init.setHomeInfo(homeInfo);
        init.setLogoInfo(logoInfo);

        //声明左侧导航栏容器
        List<SyMenuY> oneList = new ArrayList<>();

        List<SyMenuY> menu = null;
        //通过当前用户权限去redis进行查询
        log.info("根据当前用户登录权限去redis拿取数据");
        switch (classPoList.size()){
            case YltConstant.ROLE_ADMIN:
                menu = (ArrayList)redisUtils.get(YltConstant.REDIS_ADMIN);
                break;
            case YltConstant.ROLE_YYTEACHER:
                menu = (ArrayList)redisUtils.get(YltConstant.REDIS_YYTEACHER);
                break;
            case YltConstant.ROLE_TEACHER:
                menu = (ArrayList)redisUtils.get(YltConstant.REDIS_TEACHER);
                break;
            default:
                menu = (ArrayList)redisUtils.get(YltConstant.REDIS_USER);
        }
        //包装一级外侧的封装对象
        SyMenuY menuData = new SyMenuY();
        menuData.setTitle("Welcome to");
        menuData.setIcon("fa fa-address-book");
        menuData.setStatus(0);
        menuData.setTarget("_self");
        menuData.setHref("");
        //redis拿出的数据如果为空，就查询数据库并存入redis
        if (menu == null){
            log.info("redis里并未缓存数据,去数据库进行查询");
            List<SyMenuY> list = indexService.getParentMeun(user.getUserName());
            switch (classPoList.size()){
                case YltConstant.ROLE_ADMIN:
                    redisUtils.set(YltConstant.REDIS_ADMIN, list);
                    break;
                case YltConstant.ROLE_YYTEACHER:
                    redisUtils.set(YltConstant.REDIS_YYTEACHER,list);
                    break;
                case YltConstant.ROLE_TEACHER:
                    redisUtils.set(YltConstant.REDIS_TEACHER,list);
                    break;
                default:
                    redisUtils.set(YltConstant.REDIS_USER,list);
            }
            menuData.setChild(list);
        }else {
            menuData.setChild(menu);
        }

        oneList.add(menuData);
        init.setMenuInfo(oneList);

        return init;
    }


    @GetMapping("/test")
    public Student test3(@Validated @RequestBody Student student) {
        System.out.println(student);
        return student;
    }

}
